"""
所有基本参数的UI组件
包括：
    0.DataWidget 以[(parm_name,value),(,),...] 的形式保存
    1.int 类型参数组件
    2.str 类型参数组件
    3.list 类型参数组件
    #3.dict 类型参数组件  因为没有顺序所以舍弃掉
    4.tuple 类型参数组件
    5.vector2 类型参数组件
    6.vector3 类型参数组件
    8.vector4 类型参数组件
    9.matrix 类型参数组件
    10.动态复合增减栏,有加标签，eidt,加减按钮。
        就是以上类型的都可以一次性添加多个，比如要一次性添加多个int属性，按添加多个属性。
    11.自定义参数: 该参数类型不定，有开发者自定义。

组件参数API读取：
    所有组件对应一个节点，从节点里进行读取，使用node.parm['parm_name'].value()来读取，
    使用node.parm['parm_name'].set()来设置

"""
from PySide2.QtGui import *
from PySide2.QtWidgets import *
from PySide2.QtCore import *
from ..cgai import CGAI
from .CGAIPalette import CGAIBasePalatte
from .CGAIBaseWidget import *


class CGAIBaseParmWidget(QWidget):

    def __init__(self,*args):
        super(CGAIBaseParmWidget, self).__init__()


    def value(self):
        pass

    def set(self,*args,**kwargs):
        pass




class DataWidget(CGAIBaseParmWidget):
        """
        数据组件，传入标签名称，返回 [(parm_name,value),(,),...]
        """
        str_Signal = Signal(str)
        def __init__(self, label, data_list=[]):
            super(DataWidget, self).__init__()
            # self.text = data_list[0][0] if data_list else ''
            self.data_list = data_list
            self.main_layout = QHBoxLayout()
            self.main_layout.setAlignment(Qt.AlignLeft)
            self.main_layout.setSpacing(10)
            self.show_label = QLabel(label)
            self.show_label.setFont(QFont('', CGAI.PARM_FONT_SIZE))
            self.menu = CGAIMenu()
            self.menu.addItems(self.parmlist)
            self.menu.currentTextChanged.connect(self._item_change)
            self.main_layout.addWidget(self.show_label)
            self.main_layout.addWidget(self.menu)

            self.setLayout(self.main_layout)

        def value(self):
            return self.__value

        def _item_change(self, text):
            """
            文本设置的值为正确值
            但是滑动条要增加100倍
            :param value:
            :return:
            """
            self.text = text
            self.str_Signal.emit(self.text)
            print('menu 返回:', self.text)


# class IntWidget(CGAIBaseParmWidget):
#
#     def __init__(self,name,label,value=0,min=0,max=10):
#         super(IntWidget, self).__init__()



class IntWidget(CGAIBaseParmWidget):
    int_Signal = Signal(int)
    def __init__(self,name,label,value=0,min=0,max=10):
        super(IntWidget, self).__init__()
        self.__name = name
        self.__value = value
        self.min = min
        self.max = max
        self.main_layout = QHBoxLayout()
        self.main_layout.setSpacing(10)
        self.show_label = QLabel(label)
        self.show_label.setFont(QFont('',CGAI.PARM_FONT_SIZE))
        rex = QRegExp("[0-9]+$")
        rv = QRegExpValidator(rex,self)
        self.edit = CGAIEdit()
        self.edit.setText(str(value))
        self.edit.setFixedWidth(55)
        self.edit.setValidator(rv)
        self.s = CGAISlider(Qt.Horizontal)
        self.s.setMinimum(min)
        self.s.setMaximum(max)
        self.s.valueChanged.connect(self._slider_changed)
        self.edit.textChanged.connect(self._text_change)
        self.main_layout.addWidget(self.show_label)
        self.main_layout.addWidget(self.edit)
        self.main_layout.addWidget(self.s)
        self.setLayout(self.main_layout)

    def name(self):
        """
        返回当前参数名称
        :return:
        """
        return self.__name

    def value(self):
        """
        返回当前组件值
        :return:
        """
        return self.__value

    def set(self,value):

        self.__value = value
        self.edit.setText(str(int(value)))

    def _text_change(self,value):
        self.__value=int(value)
        # self.s.setValue(self.__value)

    def _slider_changed(self,value):
        self.__value=value
        self.edit.setText(str(value))
        self.int_Signal.emit(self.__value)


class FloatWidget(CGAIBaseParmWidget):
    float_Signal=Signal(float)
    def __init__(self,name,label,value=0,min=0,max=10):
        super(FloatWidget, self).__init__()
        self.__name = name
        self.__value=value
        self.min=min
        self.max=max*100  #浮点栏默认增大100倍
        self.main_layout=QHBoxLayout()
        self.main_layout.setSpacing(10)
        self.show_label=QLabel(label)
        self.show_label.setFont(QFont('',CGAI.PARM_FONT_SIZE))
        rex=QRegExp("[0-9\.]+$")
        rv=QRegExpValidator(rex,self)
        self.edit=CGAIEdit()
        self.edit.setText(str(value))
        self.edit.setFixedWidth(55)
        self.edit.setValidator(rv)
        self.s=CGAISlider(Qt.Horizontal)
        self.s.setMinimum(self.min)
        self.s.setMaximum(self.max)
        self.s.setSingleStep(1)
        self.s.valueChanged.connect(self._slider_changed)
        self.edit.textChanged.connect(self._text_change)
        self.main_layout.addWidget(self.show_label)
        self.main_layout.addWidget(self.edit)
        self.main_layout.addWidget(self.s)
        self.setLayout(self.main_layout)

    def name(self):
        """
        返回当前参数名称
        :return:
        """
        return self.__name

    def value(self):
        """
        返回当前组件值
        :return:
        """
        return self.__value

    def set(self,value):

        self.__value = value
        self.edit.setText(str(float(value)))

    def _text_change(self,value):
        """
        文本设置的值为正确值
        但是滑动条要增加100倍
        :param value:
        :return:
        """
        print('value:',value)
        self.__value = float(value)
        # self.s.setValue(self.__value*100)  #

    def _slider_changed(self,value):
        """
        滑动条的值传给text都要缩小100被
        :param value:
        :return:
        """
        self.__value=value*0.01
        self.edit.setText(str(round(self.__value,2)))
        self.float_Signal.emit(self.__value)
        print('float 返回:',self.__value)


class StrWidget(CGAIBaseParmWidget):
    str_Signal=Signal(str)
    def __init__(self,name,label,text='',height=50):
        super(StrWidget, self).__init__()
        self.__name = name
        self.__value = text
        self.text=text
        self.main_layout=QHBoxLayout()
        self.main_layout.setAlignment(Qt.AlignLeft)
        self.main_layout.setSpacing(10)
        self.show_label=QLabel(label)
        self.show_label.setFont(QFont('',CGAI.PARM_FONT_SIZE))
        self.edit = CGAIMultEdit()
        self.edit.setFixedWidth(100)
        self.edit.setFixedHeight(height)
        self.edit.setText(text)
        self.edit.textChanged.connect(self._text_change)
        self.main_layout.addWidget(self.show_label)
        self.main_layout.addWidget(self.edit)

        self.setLayout(self.main_layout)

    def name(self):
        """
        返回当前参数名称
        :return:
        """
        return self.__name

    def value(self):
        """
        返回当前组件值
        :return:
        """
        return self.__value

    def set(self,text):

        self.__value = text
        self.edit.setText(text)


    def _text_change(self):
        """
        文本设置的值为正确值
        但是滑动条要增加100倍
        :param value:
        :return:
        """
        self.text = self.edit.toPlainText()
        self.str_Signal.emit(self.text)
        print('str 返回:',self.text)


class ListWidget(CGAIBaseParmWidget):
    """
    列表组件，传入列表名称与一个列表，以菜单栏的形式显示
    """
    str_Signal=Signal(str)
    def __init__(self,label,parmlist=[]):
        super(ListWidget, self).__init__()
        self.text= parmlist[0] if parmlist else ''
        self.parmlist=parmlist
        self.main_layout=QHBoxLayout()
        self.main_layout.setAlignment(Qt.AlignLeft)
        self.main_layout.setSpacing(10)
        self.show_label=QLabel(label)
        self.show_label.setFont(QFont('',CGAI.PARM_FONT_SIZE))
        self.menu=CGAIMenu()
        self.menu.addItems(self.parmlist)
        self.menu.currentTextChanged.connect(self._item_change)
        self.main_layout.addWidget(self.show_label)
        self.main_layout.addWidget(self.menu)

        self.setLayout(self.main_layout)

    def _item_change(self,text):
        """
        文本设置的值为正确值
        但是滑动条要增加100倍
        :param value:
        :return:
        """
        self.text = text
        self.str_Signal.emit(self.text)
        print('menu 返回:',self.text)
















